package com.mes.controller;

import com.mes.entity.EmesoJobOrderExe;
import com.mes.entity.EmesoManuOrder;
import com.mes.entity.EmesoManuOrderJobOrder;
import com.mes.entity.User;
import com.mes.service.InformWorkService;
import com.mes.service.QualityService;
import com.yy.base.page.Message;
import com.yy.base.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 质量判定
 */
@Controller
@RequestMapping(value = "/data")
public class QualityController {
	@Autowired
	private InformWorkService service;
	@Autowired
	private QualityService qualityService;
	/**
	 * 质量判定
	 */
	@ResponseBody
	@Transactional
	@RequestMapping(value = "/submitQuality.html", method = RequestMethod.POST)
	public Message submitQuality(HttpServletRequest request, HttpServletResponse response) {
		Message message = new Message();
		try {
			String result = request.getParameter("result");
			String reason = request.getParameter("reason");
			String description = request.getParameter("description");
			String []codes = request.getParameter("codes").split(",");
			
			User user = (User)request.getSession().getAttribute("user");

			Map<String, String> map = new HashMap<String, String>();
			map.put("userId", user.getId());
			map.put("clientId", user.getClientId());
			map.put("orgId", user.getOrgId());
			map.put("result", result);
			map.put("reason", reason);
			map.put("description", description);
			for (String code : codes) {
				map.put("code", code);
				qualityService.saveQualityResult(map);
			}
			
			message.setOkMessage("提交成功！");
		} catch (Exception e) {
			e.printStackTrace();
			message.setErrorMessage("提交异常：" + e.getMessage());
		}
		return message;
	}
	
	@ResponseBody
	@RequestMapping(value = "/getQualityPageData.html", method = RequestMethod.POST)
	public Message getQualityPageData(HttpServletRequest request, HttpServletResponse response) {
		Message message = new Message();
		try {
			message.setData("reasonList", qualityService.getReasonList());
			List<Map<String, Object>> resultList = qualityService.getSelectorList("0B80A589DFD64147AD0126954CE05B8A");
			message.setData("resultList", resultList);
			message.setData("result", true);
		} catch (Exception e) {
			e.printStackTrace();
			message.setErrorMessage("获取下拉数据异常：" + e.getMessage());
		}
		return message;
	}
	
	@ResponseBody
	@RequestMapping(value = "/getJobExeInfo.html", method = RequestMethod.POST)
	public Message getJobExeInfo(HttpServletRequest request, HttpServletResponse response) {
		Message message = new Message();
		try {
			String code = request.getParameter("orderCode");
			EmesoManuOrderJobOrder info = service.getJobOrderInfo(code);
			if (info == null) {
				message.setErrorMessage("作业订单号不存在");
				return message;
			}
			if (!"Y".equals(info.getIsQuality())) {
				message.setErrorMessage("该订单无需质检");
				return message;
			}
			message.setData("qty", info.getQty());
			message.setData("status", info.getStatus());
			message.setData("routeNo", info.getRouteNo());
			message.setData("routeName", info.getRouteName());
			
			EmesoManuOrder manuOrder = service.getManuOrderInfo(info.getMaunOrderId());
			message.setData("code", manuOrder.getCode());
			message.setData("productCode", manuOrder.getProductCode());
			message.setData("productName", manuOrder.getProductName());
			message.setData("productName2", manuOrder.getProductName2());
			
			List<EmesoJobOrderExe> jobExeList = qualityService.getJobExeList(info.getId());
			if (jobExeList.isEmpty()) {
				message.setData("msg", "该订单无报工未检项目");
			} else {
				message.setData("jobExeList", ObjectUtil.listEntityToMap(jobExeList));
			}

			message.setData("result", true);
		} catch (Exception e) {
			e.printStackTrace();
			message.setErrorMessage("获取订单信息异常：" + e.getMessage());
		}
		return message;
	}
}
